package com.partner.control;

import io.rong.models.FormatType;
import io.rong.models.SdkHttpResult;

import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.partner.domain.Friend;
import com.partner.domain.MyFriend;
import com.partner.domain.MyTask;
import com.partner.domain.RongTokenResponse;
import com.partner.domain.User;
import com.partner.domain.UserAndGroup;
import com.partner.domain.Yaoqingfriend;
import com.partner.response.LoginResponse;
import com.partner.service.GroupService;
import com.partner.service.UserService;
import com.partner.util.ApiHttpClient;
import com.partner.util.CommonData;
import com.partner.util.FileUploadUtils;
import com.partner.util.IOUtil;

/**
 * 用户处理类
 * @author Jinshuan
 *
 */
@Controller
@RequestMapping("/user")
public class UserAction {
	UserService userService = new UserService();
	GroupService groupService = new GroupService();
	 public UserAction() {
		// TODO Auto-generated constructor stub
		 System.out.println("gouzao---");
	}
	 
	 //注册
	@RequestMapping("/register")
	public @ResponseBody String register(InputStream is){
		
		String json = IOUtil.inputStreamToStream(is);
		Gson gson = new Gson();
		String result = "1";
		if(json!=null){
		User user = gson.fromJson(json, User.class);
		System.out.println("是否为null"+user);
		if(user!=null){
			//从融云服务器得到token赋值给user
			try {
				SdkHttpResult res = ApiHttpClient.getToken(CommonData.app_key, CommonData.app_secret, user.getPhone(), user.getUsername(), "", FormatType.json);
				RongTokenResponse rongTokenResponse = gson.fromJson(res.getResult(), RongTokenResponse.class);
				if(rongTokenResponse.getToken()!=null){
					user.setToken(rongTokenResponse.getToken());
				}else{
					user.setToken("");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		result = userService.insertUser(user)+"";
		System.out.println(user.toString()+"--"+result);
		}
		}
		
		
		return result;
	}
	
	//登录
	@RequestMapping("/login")
	public  @ResponseBody LoginResponse login(InputStream is){
		String json = IOUtil.inputStreamToStream(is);
		Gson gson = new Gson();
//		int result = 1;
		LoginResponse loginResponse = new LoginResponse();
		if (json != null) {
			User user = gson.fromJson(json, User.class);
			if (user.getPhone() != null && user.getPassword() != null) {
				loginResponse = userService.login(user.getPhone(), user.getPassword());
//				System.out.println("返回给客户端--"+loginResponse.getUser().toString());
			}
		}
		
		return loginResponse;
	}
	
	//修改用户信息
	@RequestMapping("/updateUser")
	public @ResponseBody boolean updateUser(InputStream io){
		System.out.println("修改用户信息");
		String json = IOUtil.inputStreamToStream(io);
		Gson gson = new Gson();
//		FileUploadUtils.tranferFile(request, "img/user");
		if(json!=null&&!("".equals(json))){
			User user = gson.fromJson(json, User.class);
			System.out.println(json);
			return userService.updateUser(user);
		}
		return false;
	}
	
	//得到对应的用户的好友列表
	@RequestMapping("/getFriends")
	public @ResponseBody List<MyFriend> getFriends(InputStream is){
		System.out.println("得到用户的好友列表");
		String uid = IOUtil.inputStreamToStream(is);
		if(uid!=null&&!("".equals(uid))){
			return userService.getMyFriends(uid);
		}
		return null;
	}
	
	//根据搜索条件将好友搜索出来
	@RequestMapping("/getNotFriend")
	public @ResponseBody List<Friend> getNotFriend(InputStream is){
		String msg = IOUtil.inputStreamToStream(is);//从客户端得到信息
		if(msg!=null&&!("".equals(msg))){
			String[] s = msg.split(";");
			String phone = msg.split(";")[0];
			String condition = "";
			if(s.length>1){
				condition = s[1];
			}
			return userService.getNotFriend(phone, condition);
		}
		return null;
		
	}
	
	//根据群号或者群名称来搜索群
	@RequestMapping("/getNotGroup")
	public @ResponseBody List<UserAndGroup> getNotGroups(InputStream is){
		System.out.println("搜素相关的群");
		String msg = IOUtil.inputStreamToStream(is);
		if(msg!=null&&!("".equals(msg))){
			String[] s = msg.split(";");
			if(s.length==2){
				String phone = s[0];
				String condition = s[1];
				return userService.getNotGroups(phone, condition);
			}
		}
		return null;
	}
	
	//单方面加为好友
	@RequestMapping("/addFriend")
	public @ResponseBody boolean addFriend(InputStream is){
		System.out.println("单方面添加好友");
		String msg = IOUtil.inputStreamToStream(is);
		if(msg!=null&&!("".equals(msg))){
			String[] s = msg.split(";");
			if(s.length==4){
				String from = s[0];
				String to = s[1];
				String nickname = s[2];
				String groupname = s[3];
				return userService.addFriend(from, to, groupname, nickname);
			}
		}
		
		return false;
		
	}
	
	//是否有好友加我
	@RequestMapping("/getFriendNotice")
	public @ResponseBody List<User> getFriendNotice(InputStream is){
		System.out.println("好友添加消息");
		String msg = IOUtil.inputStreamToStream(is);
		if(msg!=null&&!("".equals(msg))){
			String phone = msg;
			return userService.getFriendNotice(phone);
		}
		return null;
	}
	
	//是否有群加我
	@RequestMapping("/getGroupNotice")
	public @ResponseBody List<Yaoqingfriend> getGroupNotice(InputStream is){
		System.out.println("邀请进入群");
		String msg = IOUtil.inputStreamToStream(is);
		if(msg!=null&&!("".equals(msg))){
			String phone = msg;
			return userService.getGroupNotice(phone);
		}
		return null;
	}
	//同意好友加我
	@RequestMapping("/agreeFriend")
	public @ResponseBody boolean agreeFriend(InputStream is){
		System.out.println("同意好友加我");
		String msg = IOUtil.inputStreamToStream(is);
		if(msg!=null&&!("".equals(msg))){
			String[] s = msg.split(";");
			if(s.length==4){
				String from = s[0];
				String to = s[1];
				String nickname = s[2];
				String groupname = s[3];
				return userService.agreeFriend(from, to,nickname,groupname);
			}
		}
		
		return false;
	}
	
	//如果明确是拒绝
	@RequestMapping("/declineAddFriend")
	public @ResponseBody boolean declineAddFriend(InputStream is){
		System.out.println("明确拒绝");
		String msg = IOUtil.inputStreamToStream(is);
		if(msg!=null&&!("".equals(msg))){
			String[] s = msg.split(";");
			if(s.length==2){
				String from = s[0];
				String to = s[1];
				System.out.println("from--"+from+"--to--"+to);
				return userService.declineAddFriend(from, to);
			}
		}
		return false;
	}
	
	//得到对应用户的群组
	@RequestMapping("/getGroup")
	public @ResponseBody List<UserAndGroup> getGroups(InputStream is){
		System.out.println("得到群组消息");
		String msg = IOUtil.inputStreamToStream(is);
		if(msg!=null&&!("".equals(msg))){
			String userid = msg;
			return groupService.selectUserBelongGroup(userid);
		}
		return null;
	}
	
	//加入群
	@RequestMapping("/joinGroup")
	public @ResponseBody boolean joinGroup(InputStream is){
		System.out.println("加入群");
		String msg = IOUtil.inputStreamToStream(is);
		if(msg!=null&&!("".equals(msg))){
			String[] msg_arr = msg.split(";");
			String userId = msg_arr[0];
			String groupId = msg_arr[1];
			String groupName = msg_arr[2];
			String nickName = msg_arr[3];
			return userService.joinGroup(userId, groupId, groupName, nickName);
		}
		
		return false;
	}
@RequestMapping("/selectMyTask")
	public @ResponseBody List<MyTask> selectMyTask(InputStream is){
	System.out.println("获得我的任务");
		String msg = IOUtil.inputStreamToStream(is);
		if(msg!=null&&!("".equals(msg))){
			String[] str_arr = msg.split(";");
			if(str_arr.length==2){
				int pageNow = Integer.parseInt(str_arr[1]);
				return  userService.selectMyTask(str_arr[0],pageNow,10);
			}
		}
		return null;
	}

//删除我的对应一项任务
@RequestMapping("/deleteTask")
public @ResponseBody boolean deleteMyTask(InputStream is){
	System.out.println("删除任务");
	String msg = IOUtil.inputStreamToStream(is);
	if(msg!=null&&!("".equals(msg))){
		String[] str_arr = msg.split(";");
		if(str_arr.length==2){
			System.out.println(str_arr[0]+"--"+str_arr[1]);
			return  userService.deleteMyTask(str_arr[0], str_arr[1]);
		}
	}
	return false;
}
//增加一个任务
@RequestMapping("/addTask")
public @ResponseBody boolean addTask(InputStream is){
	System.out.println("增加任务");
	String msg = IOUtil.inputStreamToStream(is);
	System.out.println(msg);
	if(msg!=null&&!("".equals(msg))){
		String[] str_arr = msg.split(";");
		System.out.println(str_arr.length);
		if(str_arr.length==6){
			String task = str_arr[0];
			String start_time = str_arr[1];
			String end_time = str_arr[2];
			String questcode = str_arr[3];
			String create_time = str_arr[4];
			String phone = str_arr[5];
			return userService.addTask(task, start_time, end_time, questcode, create_time, phone);
		}
	}
	
	return false;
}

//更改任务的状态
@RequestMapping("/updateTask")
public @ResponseBody boolean updateTask(InputStream is){
	System.out.println("修改任务");
	String msg = IOUtil.inputStreamToStream(is);
	if(msg!=null&&!("".equals(msg))){
		System.out.println(msg);
		String[] str_arr = msg.split(";");
		if(str_arr.length==3){
			String state = str_arr[0];
			String phone = str_arr[1];
			String create_time = str_arr[2];
			return userService.updateTask(state, phone, create_time);
		}
}
	return false;
}

//查询所有不是对应群的我的好友
@RequestMapping("/getMyFriendsNotInGroup")
public @ResponseBody List<MyFriend> getMyFriendsNotInGroup(InputStream is){
	System.out.println("查询所有不是对应群的我的好友");
	String msg = IOUtil.inputStreamToStream(is);
	if(msg!=null&&!("".equals(msg))){
		String str[] = msg.split(";");
		return userService.getMyFriendsNotInGroup(str[0], str[1]);
	}
	return null;
}

//去修改密码
@RequestMapping("/updatePassword")
public @ResponseBody int updatePassword(InputStream is){
	System.out.println("修改密码");
	String msg = IOUtil.inputStreamToStream(is);
	if(msg!=null&&!("".equals(msg))){
		String[] str_arr = msg.split(";");
		if(str_arr.length==3){
			//返回1就是成功修改，返回2就是密码不对。返回0就是各种各样的问题
			return userService.updatePassword(str_arr[0], str_arr[1], str_arr[2]);
		}
	}
	
	return 0;
			
}
//找回绑定的手机号
@RequestMapping("/findBindphone")
public @ResponseBody String findBindphone(InputStream is){
	System.out.println("找到绑定的号码");
	String msg = IOUtil.inputStreamToStream(is);
	if(msg!=null&&!("".equals(msg))){
		return userService.findBindPhone(msg);
	}
	
	return "";
			
}
//忘记密码之后修改密码
@RequestMapping("/updatePwd")
public @ResponseBody boolean updatePwd(InputStream is){
	System.out.println("找回密码");
	String msg = IOUtil.inputStreamToStream(is);
	if(msg!=null&&!("".equals(msg))){
		String[] str_arr = msg.split(";");
		if(str_arr.length==2){
			return userService.updatePwd(str_arr[0], str_arr[1]);
		}

}
	return false;
}

//修改用户头像
@RequestMapping(value = "/updateUserIcon",method = RequestMethod.POST)
public @ResponseBody boolean updateUserIcon(HttpServletRequest request){
	System.out.println("修改用户头像");
	String phone = request.getParameter("phone");
	String img = request.getParameter("img");
	FileUploadUtils.tranferFile(request, "img/user");
	return userService.updateUserImgIcon(phone, img);
	
}
}
